home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 August: Tool Chest / Dev.CD Aug 98 TC.toast / Tool Chest / Testing & Debugging / Virtual User / Virtual User Current Release / Examples / Example Scripts / MessagePassing.vu < prev    next >
Encoding:
Text File  |  1998-06-04  |  6.3 KB  |  246 lines  |  [TEXT/MPS ]

  1. #
  2. #    File:        MessagePassingTest.vu
  3. #
  4. #    Contains:    Demonstration of message passing between multiple targets.
  5. #
  6. #    Libraries:    none
  7. #
  8. #    Prerequisites: Key Caps needs to be installed on the targets.
  9. #
  10. #    Written by:    Rick Violet
  11. #
  12. #    Copyright:    © 1990-1992 by Apple Computer, Inc., all rights reserved.
  13. #
  14. #    Change History:
  15. #
  16. #         6/19/92    DGG            Cleaned up, made parametric, added KeyCaps interface
  17. #         ?????        Rick        Created
  18. #
  19. #    To Do:
  20. #
  21.  
  22. (************************************************************************************
  23. * Task GetMyActorName()
  24. *    Get the current actor's name
  25. ************************************************************************************)
  26. task GetMyActorName()
  27. Begin
  28.     match[Actor t:?myName];    
  29.     return myName;    
  30. end;
  31.  
  32. (************************************************************************************
  33. * Task GetAllOtherActorsNames()
  34. *    Get all of the other actors names, taking out the occurrance of the current 
  35. *    actor's name.
  36. ************************************************************************************)
  37. task GetAllOtherActorsNames()
  38. Begin
  39.     match[Actor t:?myName];    
  40.     match[Actor t:$otherNames ];
  41.     For i := 1 to Card otherNames
  42.         if otherNames[i] = myName
  43.             begin
  44.                 otherNames := remove( i, otherNames );
  45.             end;
  46.     return otherNames;    
  47. end;
  48.  
  49. (************************************************************************************
  50. * Task OpenChannel()
  51. *    This task will attempt to open a session with a given actor for communication.
  52. ************************************************************************************)
  53. task OpenChannel( receiver := undefined, tries := 10 )
  54. begin
  55.     if receiver = undefined
  56.     begin
  57.         println "Task OpenChannel - parameter 1 not declared";
  58.         return false;
  59.     end;
  60.     else if (typeOf( receiver ) <> 'string')
  61.     begin
  62.         println "Task OpenChannel - parameter 1 is not of type string";
  63.         return false;
  64.     end;
  65.     else 
  66.     begin
  67.         ActorDesc := match[ Actor t:receiver ]!;
  68.         if not ActorDesc
  69.         begin
  70.             println "Task OpenChannel - Failed to match Actor: {receiver}";
  71.             return false;
  72.         end;
  73.     end;
  74.  
  75.     while (( tries > 0 ) and not ( status = "open" ))
  76.     begin
  77.         tries := tries - 1;
  78.         status := openSession( ActorDesc );
  79.     end;
  80.  
  81.     if status = "open"
  82.     begin
  83.         return true;
  84.     end;
  85.     else
  86.     begin
  87.         println "Failed to open channel with Actor : {receiver}";
  88.         return false;
  89.     end;
  90. end;
  91.  
  92. (************************************************************************************
  93. * Task CloseChannel()
  94. *    This task will try to close a session with the specified receiver.
  95. ************************************************************************************)
  96. task CloseChannel( receiver, tries := 1 )
  97. begin
  98.     if receiver = undefined
  99.     begin
  100.         println "Task CloseChannel - parameter 1 not declared";
  101.         return false;
  102.     end;
  103.     else if (typeOf( receiver ) <> 'string')
  104.     begin
  105.         println "Task CloseChannel - parameter 1 is not of type string";
  106.         return false;
  107.     end;
  108.     else 
  109.     begin
  110.         ActorDesc := match[ Actor t:receiver ]!;
  111.         if not ActorDesc
  112.         begin
  113.             println "Task CloseChannel - Failed to match Actor: {receiver}";
  114.             return false;
  115.         end;
  116.     end;
  117.     
  118.     while ( tries > 0 ) and not ( status = "done" )
  119.     begin
  120.         tries := tries - 1;
  121.         status := closeSession( ActorDesc );
  122.     end;
  123.         
  124.     if status = "done"
  125.         return true;
  126.     else
  127.     begin
  128.         println "Failed to close channel with Actor : {receiver}";
  129.         return false;
  130.     end;
  131. end;
  132.  
  133. (************************************************************************************
  134. * Task SortList()
  135. *    Simple bubble sort to make sure that all of the actors have the same list!
  136. ************************************************************************************)
  137. task SortList( theList )
  138. begin
  139.     noChanges := false;
  140.     while not noChanges
  141.     begin
  142.         noChanges := true;
  143.         for i := 1 to (card theList - 1)
  144.             if theList[i] > theList[i+1]
  145.             begin
  146.                 tempMember := theList[i];
  147.                 theList := replace( theList[i+1], i, theList );
  148.                 theList := replace( tempMember, i+1, theList );
  149.                 noChanges := false;
  150.             end;
  151.     end;
  152.     return theList;
  153. end;
  154.  
  155. (************************************************************************************
  156. * Main Script Here!
  157. ************************************************************************************)
  158. script MessageControl(theLeader := 1, typingSpeed := 35)
  159. begin
  160.  
  161.     # Let's type pretty fast.
  162.     typeSpeed(typingSpeed);
  163.  
  164.     # These are the variables for the different messages being passed.
  165.     AllDoneFlag    := "StopTest";
  166.     messageList := {     { 1, 2, 3, "start!" },
  167.                         { "You say yes…", "I say no…", "You say stop…", "And I say go!" },
  168.                         { "ahem" },
  169.                         { "How can I be sure?", "When your intrusion is my illusion" },
  170.                         { "How can I be sure", "When all the time you changed my mind" },
  171.                         { "I asked for more and more", "How can I be sure" },
  172.                         { "ahem" },
  173.                         { AllDoneFlag}
  174.                     };
  175.     totalMessages := card messageList;
  176.     
  177.     # Get and sort all of the actors playing our game.
  178.     match [Actor t:$allActors];
  179.     allActors := SortList(allActors);
  180.  
  181.     # Choose the leader of the game
  182.     MasterDesc := match [actor t:allActors[theLeader]]!;
  183.  
  184.     myName := GetMyActorName();
  185.     println "I am ", myName;
  186.  
  187.     otherNames := GetAllOtherActorsNames();
  188.  
  189.     # Bring up KeyCaps to make things more visually interesting and so you know that
  190.     #    info is really being sent.
  191.     select [menuItem t:"Key Caps" m:[menu o:1]];
  192.  
  193.     # Start the game.
  194.     for each player in otherNames
  195.         OpenChannel(player);
  196.     
  197.     # The main part of the game, the master gives orders and the slaves obey!
  198.     if (myName = allActors[theLeader])
  199.     begin
  200.         type k:{returnKey, "I am in control!"};
  201.         msgCount := 1;
  202.         for each singleMessage in messageList
  203.         begin
  204.             for each player in otherNames
  205.             begin
  206.                 SlaveDesc := match [actor t:player];
  207.                 type k:{returnKey, "Out # {msgCount} of {totalMessages}."};
  208.                 wait(1);
  209.                 print SlaveDesc.t, " msg {msgCount} of {totalMessages}...";
  210.                 if send( SlaveDesc, singleMessage )
  211.                     println "Message sent Ok.";
  212.                 else
  213.                     println "Message send failed.";
  214.             end;
  215.             msgCount := msgCount + 1;
  216.         end;
  217.     end;
  218.     else
  219.     begin
  220.         type k:{returnKey, "I am just a slave!"};
  221.         MoreToGo := true;
  222.         while MoreToGo
  223.         begin
  224.             wait(3);
  225.             msg := receive( MasterDesc );
  226.             println "message = .", msg, ".";
  227.             type k:{returnKey, "In: ", msg};
  228.             if msg = AllDoneFlag
  229.                 MoreToGo := false;
  230.         end;
  231.     end;
  232.     
  233.     # Clean up after the game.
  234.     for each player in otherNames
  235.     begin
  236.         CloseChannel(player);
  237.     end;
  238.     
  239.     # Get rid of KeyCaps.
  240.     close [window o:1];
  241.  
  242.     println "All Done";
  243.  
  244.     ### Finished!
  245.     
  246. end; # MessageControl